﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataObjects.Interface.Production;
using BusinessObjects.Production;
using DataObjects.AdoNet;
using System.Data;

namespace DataObjects.SqlServer.Production
{
    public class SqlServerInwardOutwardDetailDao : IInwardOutwardDetailDao
    {
        public List<InwardOutwardDetail> GetInwardOutwardDetailsByRefId(string refId)
        {
            string procedures = "Get_All_InwardOutwardDetails_ByCondition";

            if (refId == "")
            {
                object[] parms = { "@WhereClause", " WHERE RefID LIKE NULL" };
                return Db.ReadList(procedures, true, Make, parms);
            }
            else
            {
                object[] parms = { "@WhereClause", " WHERE RefID LIKE '%" + refId + "%'" };
                return Db.ReadList(procedures, true, Make, parms);
            }
        }

        public void InsertInwardOutwardDetail(InwardOutwardDetail inwardOutwardDetail)
        {
            string sql = "Insert_InwardOutwardDetail";

            inwardOutwardDetail.RefID = Db.Insert(sql, true, Take(inwardOutwardDetail));

        }

        public void DeleteInwardOutwardDetailByRefId(InwardOutwardDetail inwardOutwardDetail)
        {
            string sql = "Proc_Delete_InwardOutwardDetail_ByRefID";

            object[] parms = { "@RefID", inwardOutwardDetail.RefID };

            Db.Update(sql, true, parms);
        }

        private object[] Take(InwardOutwardDetail inwardOutwardDetail)
        {
            return new object[]  
            {
                "@RefDetailID", inwardOutwardDetail.RefDetailID,
                "@RefID", inwardOutwardDetail.RefID,
                "@RestaurantMenuID", inwardOutwardDetail.RestaurantMenuID,
                "@InventoryItemID", inwardOutwardDetail.InventoryItemID,
                "@InventoryItemName", inwardOutwardDetail.InventoryItemName,
                "@Description", inwardOutwardDetail.Description,
                "@StockID", inwardOutwardDetail.StockID,
                "@UnitID", inwardOutwardDetail.UnitID,
                "@UnitConvertID", inwardOutwardDetail.UnitConvertID,
                "@Quantity", inwardOutwardDetail.Quantity,
                "@QuantityConvert", inwardOutwardDetail.QuantityConvert,
                "@UnitPrice", inwardOutwardDetail.UnitPrice,
                "@UnitPriceConvert", inwardOutwardDetail.UnitPriceConvert,
                "@Amount", inwardOutwardDetail.Amount,
                "@AmountConvert", inwardOutwardDetail.AmountConvert,
                "@OutwardPurpose", inwardOutwardDetail.OutwardPurpose
            };
        }

        private static Func<IDataReader, InwardOutwardDetail> Make = reader =>
            new InwardOutwardDetail
            {
                RefDetailID = reader["RefDetailID"].AsString(),
                RefID = reader["RefID"].AsString(),
                RestaurantMenuID = reader["RestaurantMenuID"].AsString(),
                InventoryItemID = reader["InventoryItemID"].AsString(),
                InventoryItemName = reader["InventoryItemName"].AsString(),
                Description = reader["Description"].AsString(),
                StockID = reader["StockID"].AsString(),
                UnitID = reader["UnitID"].AsString(),
                UnitConvertID = reader["UnitConvertID"].AsString(),
                Quantity = reader["Quantity"].AsDecimal(),
                QuantityConvert = reader["QuantityConvert"].AsDecimal(),
                UnitPrice = reader["UnitPrice"].AsDecimal(),
                UnitPriceConvert = reader["UnitPriceConvert"].AsDecimal(),
                Amount = reader["Amount"].AsDecimal(),
                AmountConvert = reader["AmountConvert"].AsDecimal(),
                OutwardPurpose = reader["OutwardPurpose"].AsInt(),
            };
    }
}
